#!/bin/sh

# convert a .SlackBuild to a .petbuild

usage() {
	echo "1: Just pass the SlackBuild tarball as the parameter
eg:
./slackbuild2petbuild nrg2iso.tar.gz

2: Pass the actaul SlackBuild script as the parameter
eg:
./slackbuild2petbuild samba.SlackBuild

With #2 you may need the URL for the download; if so pass it as
the next parameter:
eg:
./slackbuild2petbuild samba.SlackBuild http://www.samba.org/samba/ftp/stable/"
	exit 0
}

[ "$1" ] && PKG=${1%%.*} BUILD=$1 || usage
[ "$2" ] && URL="$2"
DIR=zconvert
PKGDIR=${DIR}/pkgs/${PKG}
[ -d "$DIR" ] || mkdir -p $DIR
rm -r ${PKGDIR}/* 2>/dev/null

sanity() {
	case $1 in
	*tar.gz|*SlackBuild)
		echo $1 | grep -q "SlackBuild$" "$1"
		if [ "$?" -eq 0 ];then
			[ "$2" ] && echo "$2" | grep -q "^http" "$2" || usage
		fi
	;;
	*) usage ;;
	esac
}

extract_targz() {
	tar -xzvf "$1" -C ${DIR}
}

convert() {
	# can get some useful info
	[ -f "${DIR}/${PKG}/${PKG}.info" ] && . ${DIR}/${PKG}/${PKG}.info
	[ -z "$DOWNLOAD" ] || URL=${DOWNLOAD%/*}
	[ -z "$DOWNLOAD" ] || COMP=tar.${DOWNLOAD##*.}
	[ -f ${DIR}/${PKG}/slack-desc ] && \
	DESC=$(while read line;do P=`echo $line | grep "^$PKG"`; [ -z "$P" ] \
&& continue; [ "$P" ] && echo "$P"	|awk -F'(' '{print $2}'\
|awk -F')' '{print $1}';break;done < ${DIR}/${PKG}/slack-desc)
	[ -z "$REQUIRES" ] ||\
	DEPENDS=`echo $REQUIRES|sed -i -e 's% %\,\+%' \
									-e 's%^[a-z]%+[a-z]%' \
									-e 's%\,$%%'`
	[ -z "$Category" ] && Category=Utility

cat > ${DIR}/${PKG}/${PKG}.petbuild << _EOF
# $PKG
# Builds from https://github.com/puppylinux-woof-CE/petbuilds

. ../../func
. ../../build.conf

URL=$URL
PKG=$PKG
VER=$VERSION
COMP=$COMP
DESC="$DESC"
DEPS="$DEPENDS"
CAT="$Category"
DESKTOP=$DESKTOP_FILE

ARCH=\$(uname -m)
case \$ARCH in
 *64) LIBDIR=lib64 ;;
 *) LIBDIR=lib ;;
esac

build() {
	# since not X compiling forget build triplet
	cd \${PKG}-\${VER}
	./configure --prefix=/usr \\
				--localstatedir=/var \\
				--sysconfdir=/etc \\
				--libdir=/usr/\$LIBDIR
	[ "$?" -eq 0 ] || exit
	make \$MKFLG
	[ "\$?" -eq 0 ] || exit
	make DESTDIR=\$CWD/\${PKG}-install install
	cd -
}
	
package() {
	# add this recipe
	install -d -m 0755 ./\$PKG-\$VER-\$ARCH/usr/share/doc
	cat ${PKG}.petbuild > ./\$PKG-\$VER-\$ARCH/usr/share/doc/\$PKG-build-recipe
	if [ "\$DESKTOP" ];then
		install -d -m 0755 ./\$PKG-\$VER-\$ARCH/usr/share/applications
		cat \$DESKTOP > ./\$PKG-\$VER-\$ARCH/usr/share/applications/\$DESKTOP
	fi
	# delete any icon cache or library cache
	find ./\$PKG-\$VER-\$ARCH -type f -name '*cache' -delete
	[ -f ./pinstall.sh ] && install -m 0755 pinstall.sh ./\$PKG-\$VER-\$ARCH/
	[ -f ./puninstall.sh ] && install -m 0755 puninstall.sh ./\$PKG-\$VER-\$ARCH/
	for p in \$(ls|grep "\\-\${ARCH}"|grep -v "files\$") ; do
		case \$p in
			*_DEV*) DESC="\$PKG development"; DEPS=+\${PKG} ;;
			*_DOC*) DESC="\$PKG documentation"; DEPS=+\${PKG} ;;
			*_NLS*) DESC="\$PKG locales"; DEPS=+\${PKG} ;;
		esac	
		echo "packaging \$p"	
		dir2pet -x -s -w="\$DESC" -d="\$DEPS" -c=\$CAT -p=\${p} 2>&1 >/dev/null
		rm -r \$p
		mv \${p}.pet ../0pets_out
	done
	rm -r \${PKG}-\${VER}
	rm *.files 2>/dev/null
	echo "done!"
}

# main
retrieve \${PKG}-\${VER}.\${COMP}
extract \${PKG}-\${VER}.\${COMP}
build
package	
_EOF
	
}

mv_to_queue() {
	PB=`ls ${PKGDIR} | grep "petbuild$"`
	[ -z "$PB" ] && echo "error" && exit 1
	mkdir -p ${PB%.*}
	cat $DIR//$PKG/$PB > ${PB%.*}/$PB
	echo ${PB%.*} >> ORDER
	echo "Please check and adjust ${PB%.*}/$PB for sanity."
	echo "done!"
}

# main
sanity $BUILD $URL
if echo $BUILD | grep -q "tar\.gz$";then
	extract_targz $BUILD
else
	mkdir -p $PKGDIR && cp -a $BUILD $PKGDIR/
fi
convert
mv_to_queue
